import pygame
import random
import sys
import math
from enum import Enum

# 初始化pygame
pygame.init()

# 游戏设置
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (128, 128, 128)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
PURPLE = (128, 0, 128)
CYAN = (0, 255, 255)
PINK = (255, 192, 203)
DARK_RED = (139, 0, 0)
LIGHT_BLUE = (173, 216, 230)

# 游戏元素尺寸
PLAYER_SIZE = 20
ENEMY_SIZE = 25
PLAYER_BULLET_SIZE = 6
ENEMY_BULLET_SIZE = 4
POWERUP_SIZE = 20

# 游戏状态
class GameState(Enum):
    MENU = 1
    PLAYING = 2
    PAUSED = 3
    GAME_OVER = 4

# 敌人类型
class EnemyType(Enum):
    BASIC = 1      # 基础敌人 - 直线移动
    CIRCLE = 2     # 环形移动敌人
    ZIGZAG = 3     # Z字形敌人
    SNIPER = 4     # 狙击手 - 精确射击
    SPREAD = 5     # 散弹敌人 - 扇形弹幕

# 道具类型
class PowerUpType(Enum):
    RAPID_FIRE = 1  # 快速射击
    TRIPLE_SHOT = 2 # 三连发
    SHIELD = 3      # 护盾
    SLOW_TIME = 4   # 时间减缓
    BOMB = 5        # 清屏炸弹

class Player:
    def __init__(self):
        self.x = SCREEN_WIDTH // 2
        self.y = SCREEN_HEIGHT - 100
        self.size = PLAYER_SIZE
        self.speed = 5
        self.rect = pygame.Rect(self.x - self.size, self.y - self.size,
                              self.size * 2, self.size * 2)
        self.bullets = []
        self.fire_cooldown = 0
        self.fire_rate = 10
        self.lives = 3
        self.invulnerable_timer = 0
        self.powerup_timer = 0
        self.current_powerup = None
        self.shield_active = False
        self.shield_timer = 0
        self.animation_timer = 0

    def update(self):
        # 更新冷却时间
        if self.fire_cooldown > 0:
            self.fire_cooldown -= 1

        # 更新无敌时间
        if self.invulnerable_timer > 0:
            self.invulnerable_timer -= 1

        # 更新道具时间
        if self.powerup_timer > 0:
            self.powerup_timer -= 1
            if self.powerup_timer == 0:
                self.current_powerup = None
                self.fire_rate = 10

        # 更新护盾时间
        if self.shield_timer > 0:
            self.shield_timer -= 1
            if self.shield_timer == 0:
                self.shield_active = False

        # 更新动画
        self.animation_timer += 1

        # 更新子弹
        for bullet in self.bullets[:]:
            bullet.update()
            if bullet.y < -10:
                self.bullets.remove(bullet)

    def move_left(self):
        self.x = max(self.size, self.x - self.speed)
        self.rect.x = self.x - self.size

    def move_right(self):
        self.x = min(SCREEN_WIDTH - self.size, self.x + self.speed)
        self.rect.x = self.x - self.size

    def move_up(self):
        self.y = max(self.size, self.y - self.speed)
        self.rect.y = self.y - self.size

    def move_down(self):
        self.y = min(SCREEN_HEIGHT - self.size, self.y + self.speed)
        self.rect.y = self.y - self.size

    def fire(self):
        if self.fire_cooldown <= 0:
            if self.current_powerup == PowerUpType.TRIPLE_SHOT:
                # 三连发
                self.bullets.append(Bullet(self.x - 10, self.y - self.size, False))
                self.bullets.append(Bullet(self.x, self.y - self.size, False))
                self.bullets.append(Bullet(self.x + 10, self.y - self.size, False))
                self.fire_cooldown = self.fire_rate
            else:
                # 普通射击
                self.bullets.append(Bullet(self.x, self.y - self.size, False))
                self.fire_cooldown = self.fire_rate

    def apply_powerup(self, powerup_type):
        self.current_powerup = powerup_type
        self.powerup_timer = 300  # 5秒

        if powerup_type == PowerUpType.RAPID_FIRE:
            self.fire_rate = 3
        elif powerup_type == PowerUpType.SHIELD:
            self.shield_active = True
            self.shield_timer = 300
        elif powerup_type == PowerUpType.SLOW_TIME:
            # 时间减缓效果会在游戏主循环中处理
            pass

    def take_damage(self):
        if self.invulnerable_timer <= 0 and not self.shield_active:
            self.lives -= 1
            self.invulnerable_timer = 120  # 2秒无敌时间
            return True
        elif self.shield_active:
            self.shield_timer -= 60  # 护盾抵挡一次
            if self.shield_timer <= 0:
                self.shield_active = False
            return False
        return False

    def draw(self, screen):
        # 绘制护盾
        if self.shield_active:
            pygame.draw.circle(screen, CYAN, (self.x, self.y),
                             self.size + 15, 2)

        # 绘制玩家
        color = WHITE
        if self.invulnerable_timer > 0 and self.invulnerable_timer % 10 < 5:
            color = LIGHT_BLUE

        # 绘制飞机形状
        points = [
            (self.x, self.y - self.size),
            (self.x - self.size, self.y + self.size),
            (self.x, self.y + self.size // 2),
            (self.x + self.size, self.y + self.size)
        ]
        pygame.draw.polygon(screen, color, points)

        # 绘制引擎火焰
        if self.animation_timer % 4 < 2:
            flame_points = [
                (self.x - 5, self.y + self.size),
                (self.x, self.y + self.size + 8),
                (self.x + 5, self.y + self.size)
            ]
            pygame.draw.polygon(screen, ORANGE, flame_points)

        # 绘制子弹
        for bullet in self.bullets:
            bullet.draw(screen)

class Enemy:
    def __init__(self, enemy_type=EnemyType.BASIC):
        self.type = enemy_type
        self.size = ENEMY_SIZE
        self.x = random.randint(self.size, SCREEN_WIDTH - self.size)
        self.y = random.randint(-100, -50)
        self.rect = pygame.Rect(self.x - self.size, self.y - self.size,
                              self.size * 2, self.size * 2)
        self.bullets = []
        self.fire_cooldown = 0
        self.animation_timer = 0
        self.movement_timer = 0
        self.setup_movement()

    def setup_movement(self):
        if self.type == EnemyType.BASIC:
            self.speed = 2
            self.fire_rate = 80
        elif self.type == EnemyType.CIRCLE:
            self.speed = 1.5
            self.fire_rate = 60
            self.circle_center_x = SCREEN_WIDTH // 2
            self.circle_center_y = SCREEN_HEIGHT // 3
            self.circle_radius = 150
            self.circle_angle = 0
        elif self.type == EnemyType.ZIGZAG:
            self.speed = 2.5
            self.fire_rate = 70
            self.zigzag_direction = 1
        elif self.type == EnemyType.SNIPER:
            self.speed = 1
            self.fire_rate = 40
        elif self.type == EnemyType.SPREAD:
            self.speed = 1.5
            self.fire_rate = 90

    def update(self, player_x, player_y):
        self.animation_timer += 1
        self.movement_timer += 1

        # 更新位置
        if self.type == EnemyType.BASIC:
            self.y += self.speed
        elif self.type == EnemyType.CIRCLE:
            self.circle_angle += 0.02
            self.x = self.circle_center_x + self.circle_radius * math.cos(self.circle_angle)
            self.y = self.circle_center_y + self.circle_radius * math.sin(self.circle_angle) + self.movement_timer * 0.5
        elif self.type == EnemyType.ZIGZAG:
            self.y += self.speed
            self.x += self.zigzag_direction * 2
            if self.x <= self.size or self.x >= SCREEN_WIDTH - self.size:
                self.zigzag_direction *= -1
        elif self.type == EnemyType.SNIPER:
            # 狙击手缓慢移动，主要瞄准玩家
            dx = player_x - self.x
            dy = player_y - self.y
            distance = math.sqrt(dx**2 + dy**2)
            if distance > 0:
                self.x += (dx / distance) * self.speed
                self.y += (dy / distance) * self.speed * 0.3
        elif self.type == EnemyType.SPREAD:
            self.y += self.speed
            self.x += math.sin(self.movement_timer * 0.05) * 3

        self.rect.x = self.x - self.size
        self.rect.y = self.y - self.size

        # 更新子弹
        for bullet in self.bullets[:]:
            bullet.update()
            if bullet.y > SCREEN_HEIGHT + 10:
                self.bullets.remove(bullet)

        # 更新射击冷却
        if self.fire_cooldown > 0:
            self.fire_cooldown -= 1

        # 射击逻辑
        if self.fire_cooldown <= 0:
            self.fire(player_x, player_y)

    def fire(self, player_x, player_y):
        if self.type == EnemyType.BASIC:
            self.bullets.append(Bullet(self.x, self.y + self.size, True))
            self.fire_cooldown = self.fire_rate
        elif self.type == EnemyType.SNIPER:
            # 狙击手精确瞄准
            dx = player_x - self.x
            dy = player_y - self.y
            distance = math.sqrt(dx**2 + dy**2)
            if distance > 0:
                vx = (dx / distance) * 8
                vy = (dy / distance) * 8
                self.bullets.append(Bullet(self.x, self.y + self.size, True, vx, vy))
            self.fire_cooldown = self.fire_rate
        elif self.type == EnemyType.SPREAD:
            # 散弹射击
            for angle in range(-30, 31, 15):
                rad = math.radians(angle)
                vx = math.sin(rad) * 6
                vy = math.cos(rad) * 6
                self.bullets.append(Bullet(self.x, self.y + self.size, True, vx, vy))
            self.fire_cooldown = self.fire_rate
        else:
            # 其他敌人随机射击
            if random.random() < 0.02:
                self.bullets.append(Bullet(self.x, self.y + self.size, True))
                self.fire_cooldown = self.fire_rate

    def get_color(self):
        if self.type == EnemyType.BASIC:
            return RED
        elif self.type == EnemyType.CIRCLE:
            return PURPLE
        elif self.type == EnemyType.ZIGZAG:
            return ORANGE
        elif self.type == EnemyType.SNIPER:
            return DARK_RED
        elif self.type == EnemyType.SPREAD:
            return PINK
        return WHITE

    def draw(self, screen):
        color = self.get_color()

        # 绘制敌人形状
        if self.type == EnemyType.BASIC:
            # 简单的三角形
            points = [
                (self.x, self.y + self.size),
                (self.x - self.size, self.y - self.size),
                (self.x + self.size, self.y - self.size)
            ]
            pygame.draw.polygon(screen, color, points)
        elif self.type == EnemyType.CIRCLE:
            # 圆形敌人
            pygame.draw.circle(screen, color, (int(self.x), int(self.y)), self.size)
            pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), self.size - 5, 2)
        elif self.type == EnemyType.ZIGZAG:
            # 菱形敌人
            points = [
                (self.x, self.y - self.size),
                (self.x + self.size, self.y),
                (self.x, self.y + self.size),
                (self.x - self.size, self.y)
            ]
            pygame.draw.polygon(screen, color, points)
        elif self.type == EnemyType.SNIPER:
            # 狙击手 - 方形
            pygame.draw.rect(screen, color, self.rect)
            pygame.draw.rect(screen, WHITE, self.rect, 2)
            # 瞄准线
            pygame.draw.line(screen, RED, (self.x, self.y + self.size),
                           (self.x, self.y + self.size + 20), 1)
        elif self.type == EnemyType.SPREAD:
            # 散弹敌人 - 六边形
            points = []
            for i in range(6):
                angle = i * math.pi / 3
                x = self.x + self.size * math.cos(angle)
                y = self.y + self.size * math.sin(angle)
                points.append((x, y))
            pygame.draw.polygon(screen, color, points)

        # 绘制子弹
        for bullet in self.bullets:
            bullet.draw(screen)

class Bullet:
    def __init__(self, x, y, is_enemy=False, vx=None, vy=None):
        self.x = x
        self.y = y
        self.is_enemy = is_enemy
        self.size = ENEMY_BULLET_SIZE if is_enemy else PLAYER_BULLET_SIZE

        if is_enemy:
            if vx is not None and vy is not None:
                self.vx = vx
                self.vy = vy
            else:
                self.vx = 0
                self.vy = 4
        else:
            self.vy = -10
            self.vx = 0

    def update(self):
        self.x += self.vx
        self.y += self.vy

    def draw(self, screen):
        color = RED if self.is_enemy else YELLOW
        pygame.draw.circle(screen, color, (int(self.x), int(self.y)), self.size)

        # 添加发光效果
        pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), self.size + 2, 1)

class PowerUp:
    def __init__(self, x, y, power_type):
        self.x = x
        self.y = y
        self.type = power_type
        self.size = POWERUP_SIZE
        self.speed = 2
        self.rect = pygame.Rect(x - self.size, y - self.size,
                              self.size * 2, self.size * 2)
        self.animation_timer = 0

    def update(self):
        self.y += self.speed
        self.rect.y = self.y - self.size
        self.animation_timer += 1

    def get_color(self):
        if self.type == PowerUpType.RAPID_FIRE:
            return YELLOW
        elif self.type == PowerUpType.TRIPLE_SHOT:
            return GREEN
        elif self.type == PowerUpType.SHIELD:
            return CYAN
        elif self.type == PowerUpType.SLOW_TIME:
            return PURPLE
        elif self.type == PowerUpType.BOMB:
            return RED
        return WHITE

    def draw(self, screen):
        color = self.get_color()

        # 动画效果
        bounce = abs(math.sin(self.animation_timer * 0.1)) * 3

        if self.type == PowerUpType.BOMB:
            # 炸弹图标
            pygame.draw.circle(screen, color, (int(self.x), int(self.y + bounce)),
                             self.size)
            for i in range(8):
                angle = i * math.pi / 4
                x = self.x + (self.size + 5) * math.cos(angle)
                y = self.y + bounce + (self.size + 5) * math.sin(angle)
                pygame.draw.circle(screen, ORANGE, (int(x), int(y)), 3)
        else:
            # 其他道具图标
            pygame.draw.circle(screen, color, (int(self.x), int(self.y + bounce)),
                             self.size)
            pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y + bounce)),
                             self.size, 2)

class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.vx = random.uniform(-5, 5)
        self.vy = random.uniform(-5, 5)
        self.color = color
        self.life = 30
        self.size = random.randint(2, 6)

    def update(self):
        self.x += self.vx
        self.y += self.vy
        self.vy += 0.2  # 重力
        self.life -= 1
        self.size = max(1, self.size - 0.1)

    def draw(self, screen):
        if self.life > 0:
            alpha = int(255 * (self.life / 30))
            pygame.draw.circle(screen, self.color,
                             (int(self.x), int(self.y)),
                             int(self.size))

class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("弹幕射击 - Bullet Hell")
        self.clock = pygame.time.Clock()

        # 字体
        self.font_large = pygame.font.Font(None, 48)
        self.font_medium = pygame.font.Font(None, 36)
        self.font_small = pygame.font.Font(None, 24)

        # 游戏状态
        self.state = GameState.MENU

        # 游戏对象
        self.player = Player()
        self.enemies = []
        self.powerups = []
        self.particles = []

        # 游戏统计
        self.score = 0
        self.high_score = 0
        self.wave = 1
        self.enemies_spawned = 0
        self.enemies_per_wave = 5
        self.spawn_timer = 0
        self.powerup_spawn_timer = 0

        # 游戏效果
        self.slow_time_active = False
        self.slow_time_timer = 0

    def spawn_enemy(self):
        # 随着波次增加，出现更多类型的敌人
        enemy_types = [EnemyType.BASIC]

        if self.wave >= 2:
            enemy_types.append(EnemyType.CIRCLE)
        if self.wave >= 3:
            enemy_types.append(EnemyType.ZIGZAG)
        if self.wave >= 4:
            enemy_types.append(EnemyType.SNIPER)
        if self.wave >= 5:
            enemy_types.append(EnemyType.SPREAD)

        enemy_type = random.choice(enemy_types)
        self.enemies.append(Enemy(enemy_type))
        self.enemies_spawned += 1

    def spawn_powerup(self):
        if random.random() < 0.3:  # 30%概率掉落道具
            x = random.randint(50, SCREEN_WIDTH - 50)
            y = -30
            power_type = random.choice(list(PowerUpType))
            self.powerups.append(PowerUp(x, y, power_type))

    def create_explosion(self, x, y, color, count=20):
        for _ in range(count):
            self.particles.append(Particle(x, y, color))

    def handle_collisions(self):
        # 玩家子弹与敌人碰撞
        for bullet in self.player.bullets[:]:
            for enemy in self.enemies[:]:
                if bullet.y < enemy.y + enemy.size and \
                   abs(bullet.x - enemy.x) < enemy.size:
                    if bullet in self.player.bullets:
                        self.player.bullets.remove(bullet)

                    if enemy in self.enemies:
                        self.enemies.remove(enemy)
                        self.score += 100
                        self.create_explosion(enemy.x, enemy.y, enemy.get_color())
                        self.spawn_powerup()

                    break

        # 敌人子弹与玩家碰撞
        for enemy in self.enemies:
            for bullet in enemy.bullets[:]:
                if abs(bullet.x - self.player.x) < self.player.size and \
                   abs(bullet.y - self.player.y) < self.player.size:
                    if bullet in enemy.bullets:
                        enemy.bullets.remove(bullet)

                    if self.player.take_damage():
                        self.create_explosion(self.player.x, self.player.y, RED, 30)
                        if self.player.lives <= 0:
                            self.state = GameState.GAME_OVER
                            if self.score > self.high_score:
                                self.high_score = self.score
                    break

        # 玩家与敌人碰撞
        for enemy in self.enemies[:]:
            if self.player.rect.colliderect(enemy.rect):
                if self.player.take_damage():
                    self.enemies.remove(enemy)
                    self.create_explosion(self.player.x, self.player.y, RED, 30)
                    self.create_explosion(enemy.x, enemy.y, enemy.get_color())

                    if self.player.lives <= 0:
                        self.state = GameState.GAME_OVER
                        if self.score > self.high_score:
                            self.high_score = self.score

        # 玩家与道具碰撞
        for powerup in self.powerups[:]:
            if self.player.rect.colliderect(powerup.rect):
                self.player.apply_powerup(powerup.type)
                self.powerups.remove(powerup)
                self.create_explosion(powerup.x, powerup.y, powerup.get_color(), 15)

    def update(self):
        if self.state != GameState.PLAYING:
            return

        # 更新时间减缓效果
        if self.slow_time_active:
            self.slow_time_timer -= 1
            if self.slow_time_timer <= 0:
                self.slow_time_active = False

        # 更新玩家
        self.player.update()

        # 更新敌人
        speed_multiplier = 0.3 if self.slow_time_active else 1.0
        for enemy in self.enemies[:]:
            enemy.update(self.player.x, self.player.y)

            # 应用时间减缓
            if self.slow_time_active:
                enemy.x += enemy.x * 0.7 - enemy.x
                enemy.y += enemy.y * 0.7 - enemy.y
                enemy.rect.x = enemy.x - enemy.size
                enemy.rect.y = enemy.y - enemy.size
                for bullet in enemy.bullets:
                    bullet.x += bullet.vx * 0.7 - bullet.vx
                    bullet.y += bullet.vy * 0.7 - bullet.vy

            if enemy.y > SCREEN_HEIGHT + 50:
                self.enemies.remove(enemy)

        # 更新道具
        for powerup in self.powerups[:]:
            powerup.update()
            if powerup.y > SCREEN_HEIGHT + 50:
                self.powerups.remove(powerup)

        # 更新粒子
        for particle in self.particles[:]:
            particle.update()
            if particle.life <= 0:
                self.particles.remove(particle)

        # 碰撞检测
        self.handle_collisions()

        # 生成敌人
        if self.enemies_spawned < self.enemies_per_wave:
            self.spawn_timer += 1
            if self.spawn_timer >= 60:  # 每秒生成一个敌人
                self.spawn_enemy()
                self.spawn_timer = 0

        # 检查波次完成
        if self.enemies_spawned >= self.enemies_per_wave and len(self.enemies) == 0:
            self.wave += 1
            self.enemies_spawned = 0
            self.enemies_per_wave = min(15, 5 + self.wave * 2)

        # 生成道具
        self.powerup_spawn_timer += 1
        if self.powerup_spawn_timer >= 300:  # 每5秒可能生成道具
            self.spawn_powerup()
            self.powerup_spawn_timer = 0

    def draw_menu(self):
        self.screen.fill(BLACK)

        # 绘制星空背景
        for _ in range(100):
            x = random.randint(0, SCREEN_WIDTH)
            y = random.randint(0, SCREEN_HEIGHT)
            pygame.draw.circle(self.screen, WHITE, (x, y), 1)

        # 标题
        title = self.font_large.render("BULLET HELL", True, WHITE)
        title_rect = title.get_rect(center=(SCREEN_WIDTH // 2, 150))
        self.screen.blit(title, title_rect)

        # 说明
        instructions = [
            "Arrow Keys - Move",
            "SPACE - Fire",
            "ESC - Pause",
            "",
            "Survive the bullet hell!",
            "Collect power-ups for advantages",
            "",
            "Press SPACE to Start"
        ]

        y = 250
        for instruction in instructions:
            text = self.font_small.render(instruction, True, WHITE)
            text_rect = text.get_rect(center=(SCREEN_WIDTH // 2, y))
            self.screen.blit(text, text_rect)
            y += 30

    def draw_game(self):
        self.screen.fill(BLACK)

        # 绘制星空背景
        for _ in range(50):
            x = random.randint(0, SCREEN_WIDTH)
            y = random.randint(0, SCREEN_HEIGHT)
            pygame.draw.circle(self.screen, (50, 50, 50), (x, y), 1)

        # 绘制游戏对象
        self.player.draw(self.screen)

        for enemy in self.enemies:
            enemy.draw(self.screen)

        for powerup in self.powerups:
            powerup.draw(self.screen)

        for particle in self.particles:
            particle.draw(self.screen)

        # 绘制UI
        self.draw_ui()

    def draw_ui(self):
        # 分数
        score_text = self.font_medium.render(f"Score: {self.score}", True, WHITE)
        self.screen.blit(score_text, (10, 10))

        # 生命值
        lives_text = self.font_medium.render(f"Lives: {self.player.lives}", True, WHITE)
        self.screen.blit(lives_text, (10, 50))

        # 波次
        wave_text = self.font_small.render(f"Wave: {self.wave}", True, WHITE)
        self.screen.blit(wave_text, (10, 90))

        # 敌人数
        enemies_text = self.font_small.render(f"Enemies: {len(self.enemies)}", True, WHITE)
        self.screen.blit(enemies_text, (10, 120))

        # 道具状态
        if self.player.current_powerup:
            powerup_names = {
                PowerUpType.RAPID_FIRE: "Rapid Fire",
                PowerUpType.TRIPLE_SHOT: "Triple Shot",
                PowerUpType.SHIELD: "Shield",
                PowerUpType.SLOW_TIME: "Slow Time"
            }
            powerup_name = powerup_names.get(self.player.current_powerup, "")
            powerup_text = self.font_small.render(f"Power: {powerup_name}", True, YELLOW)
            self.screen.blit(powerup_text, (10, 150))

        # 时间减缓状态
        if self.slow_time_active:
            slow_text = self.font_medium.render("SLOW TIME", True, PURPLE)
            slow_rect = slow_text.get_rect(center=(SCREEN_WIDTH // 2, 100))
            self.screen.blit(slow_text, slow_rect)

        # 操作提示
        hint_text = self.font_small.render("ESC - Pause", True, GRAY)
        self.screen.blit(hint_text, (SCREEN_WIDTH - 120, 10))

    def draw_paused(self):
        self.draw_game()

        # 半透明遮罩
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.set_alpha(128)
        overlay.fill(BLACK)
        self.screen.blit(overlay, (0, 0))

        # 暂停文字
        paused_text = self.font_large.render("PAUSED", True, WHITE)
        paused_rect = paused_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
        self.screen.blit(paused_text, paused_rect)

        resume_text = self.font_medium.render("Press ESC to Resume", True, WHITE)
        resume_rect = resume_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50))
        self.screen.blit(resume_text, resume_rect)

    def draw_game_over(self):
        self.draw_game()

        # 半透明遮罩
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.set_alpha(128)
        overlay.fill(BLACK)
        self.screen.blit(overlay, (0, 0))

        # 游戏结束文字
        game_over_text = self.font_large.render("GAME OVER", True, RED)
        game_over_rect = game_over_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 50))
        self.screen.blit(game_over_text, game_over_rect)

        # 统计信息
        score_text = self.font_medium.render(f"Final Score: {self.score}", True, WHITE)
        score_rect = score_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
        self.screen.blit(score_text, score_rect)

        if self.score == self.high_score:
            high_score_text = self.font_medium.render("NEW HIGH SCORE!", True, YELLOW)
            high_score_rect = high_score_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 40))
            self.screen.blit(high_score_text, high_score_rect)

        wave_text = self.font_medium.render(f"Reached Wave: {self.wave}", True, WHITE)
        wave_rect = wave_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 80))
        self.screen.blit(wave_text, wave_rect)

        # 重新开始提示
        restart_text = self.font_medium.render("Press SPACE to Try Again", True, GREEN)
        restart_rect = restart_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 130))
        self.screen.blit(restart_text, restart_rect)

        menu_text = self.font_medium.render("Press ESC for Menu", True, WHITE)
        menu_rect = menu_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 170))
        self.screen.blit(menu_text, menu_rect)

    def draw(self):
        if self.state == GameState.MENU:
            self.draw_menu()
        elif self.state == GameState.PLAYING:
            self.draw_game()
        elif self.state == GameState.PAUSED:
            self.draw_paused()
        elif self.state == GameState.GAME_OVER:
            self.draw_game_over()

        pygame.display.flip()

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    if self.state == GameState.PLAYING:
                        self.state = GameState.PAUSED
                    elif self.state == GameState.PAUSED:
                        self.state = GameState.PLAYING
                    else:
                        return False

                if self.state == GameState.MENU:
                    if event.key == pygame.K_SPACE:
                        self.state = GameState.PLAYING
                        self.reset_game()

                elif self.state == GameState.GAME_OVER:
                    if event.key == pygame.K_SPACE:
                        self.state = GameState.PLAYING
                        self.reset_game()

        # 持续按键检测
        if self.state == GameState.PLAYING:
            keys = pygame.key.get_pressed()

            # 移动
            if keys[pygame.K_LEFT]:
                self.player.move_left()
            if keys[pygame.K_RIGHT]:
                self.player.move_right()
            if keys[pygame.K_UP]:
                self.player.move_up()
            if keys[pygame.K_DOWN]:
                self.player.move_down()

            # 射击
            if keys[pygame.K_SPACE]:
                self.player.fire()

        return True

    def reset_game(self):
        self.player = Player()
        self.enemies.clear()
        self.powerups.clear()
        self.particles.clear()
        self.score = 0
        self.wave = 1
        self.enemies_spawned = 0
        self.enemies_per_wave = 5
        self.spawn_timer = 0
        self.powerup_spawn_timer = 0
        self.slow_time_active = False
        self.slow_time_timer = 0

    def run(self):
        running = True

        while running:
            running = self.handle_events()
            self.update()
            self.draw()
            self.clock.tick(FPS)

        pygame.quit()
        sys.exit()

# 运行游戏
if __name__ == "__main__":
    game = Game()
    game.run()